import sys, json
import showdb

def get(show_name, season_number, episode_number):
	try:
		show = showdb.GetShow(show_name)
		season = show.seasons.filter(showdb.Season.q.number == int(season_number)).getOne()
		episode = season.episodes.filter(showdb.Episode.q.number == int(episode_number)).getOne()
		return show.name, episode.title
	except showdb.SQLObjectNotFound:
		return None, None
	except AttributeError:
		return None, None

def main():
	if len(sys.argv) == 4:
		show, title = get(*sys.argv[1:4])
		if title:
			print json.dumps({"show": show, "episode": title})
	else:
		sys.stderr.write("usage: %s <show> <season> <episode>\n" % sys.argv[0])

if __name__ == '__main__':
	showdb.init()
	main()
